<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Moo Service Helper
 *
 * $Id$
 *
 * @package		Service Module
 * @author		Alex Sancho
 * @copyright	(c) 2008 Alex Sancho
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *	  contributors may be used to endorse or promote products derived
 *	  from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

class moo_Core
{
	protected static $images = array();
	protected static $text = array();
	protected static $counter = 0;

	/**
	 * add_image
	 *
	 * @param mixed $url
	 * @param string $type
	 * @param array $crop
	 * @return void
	 * @access public
	 * 
	 */
	public static function add_image($url, $type = 'variable', $crop = 'auto') 
	{
		$img = (is_array($url)) ? array_merge(array('type' => $type, 'crop' => $crop), $url) : array('url' => $url, 'type' => $type, 'crop' => $crop);

		moo::$images[++moo::$counter][$img['type']] = $img;
		return moo::$counter;
	}
	
	/**
	 * add_bundle
	 * 
	 * 
	 */
	public static function add_bundle()
	{
		/* TODO */
	}
	
	/**
	 * get_images
	 *
	 * @param integer $index
	 * @param bool $url_only
	 * @return array
	 * @access public
	 * 
	 */
	public function get_images($index = null, $url_only = true) 
	{
		$tmp = (isset(moo::$images[$index])) ? moo::$images[$index] : moo::$images;

		if ($url_only === true) 
			foreach($tmp as $key => $image) 
				$tmp[$key] = $image['url'];

		return $tmp;
	}
	
	/**
	 * remove_image
	 *
	 * @param integer $index
	 * @param string $type
	 * @param integer $image
	 * @return mixed
	 * @access public
	 * 
	 */
	public function remove_image($index, $type = 'variable', $image = null) 
	{
		if (isset(moo::$images[$index][$type][$image])) 
		{
			unset(moo::$images[$index][$type][$image]);
			array_merge(moo::$images[$index]);

			return --moo::$counter;
		} 
		elseif (count(moo::$images[$index][$type]) > 0) 
		{
			array_pop(moo::$images[$index][$type]);

			return --moo::$counter;
		} 
		else 
		{
			return false;
		}
	}

	/**
	 * add_text
	 *
	 * @param integer $index
	 * @param mixed $data
	 * @param string $line
	 * @return void
	 * @access public
	 * 
	 */
	public static function add_text($index, $data, $line = null) 
	{
		if ( ! is_array($data)) 
			$data = array('string' => $data);

		$data = array_merge(array
		(
			'type'		=> 'minicard',
			'id' 		=> 1,
			'string'	=> '',
			'size'		=> '',
			'bold'		=> false,
			'italic'	=> false,
			'align'		=> 'left',
			'font'		=> 'modern',
			'colour'	=> '#000000',
		), $data);

		if ( ! empty($line)) 
			$data['id'] = $line;

		moo::$text[$index][$data['type']][$data['id']] = $data;
	}
	
	/**
	 * to_xml
	 *
	 * @param integer $index
	 * @param boolean $chooser
	 * @return string
	 * @access private
	 * 
	 */
	public static function to_xml($index, $chooser = false)
	{
		$xml = new DOMDocument();

		if ($chooser === true)
		{
			foreach(moo::$images[$index] as $type => $img) 
			{
				$root = $xml->createElement('images');
				$root->appendChild($xml->createElement('url', $img['url']));
			}
		}
		else
		{
			$root = $xml->createElement('design');
			foreach(moo::$images[$index] as $type => $img) 
			{
				$image = $xml->createElement('image');
				$crop = $xml->createElement('crop');

				if (is_array($img['crop']) and isset($img['crop']['x'], $img['crop']['y'], $img['crop']['width'], $img['crop']['height'])) 
				{
					$manual = $xml->createElement('manual');
					foreach($img['crop'] as $key => $val)
						$manual->appendChild($xml->createElement($key, $val));

					$crop->appendChild($manual);
				}
				else
				{
					$crop->appendChild($xml->createElement('auto', 'true'));
				}

				$image->appendChild($xml->createElement('url', $img['url']));
				$image->appendChild($xml->createElement('type', $type));
				$image->appendChild($crop);
				$root->appendChild($image);
			}
			
			if (count(moo::$text[$index]) > 0)
			{
				$collection = $xml->createElement('text_collection');
				foreach(moo::$text[$index] as $type => $text)
				{
					$$type = $xml->createElement($type);
					foreach($text as $id => $item) 
					{
						$line = $xml->createElement('text_line');
						foreach($item as $key => $val)
							$line->appendChild($xml->createElement($key, $val));

						$$type->appendChild($line);
					}
					$collection->appendChild($$type);
				}
				$root->appendChild($collection);
			}
		}
		return $xml->saveXML($root);
	}

} // End moo helper